<?php

/***************************************************************\
* FILE:		sys/pages/edit_item.inc
* SECTION:	pages > items
* FUNCTION:	edit items
* USES:	
* INCLUDES:	/sys/itemfunctions.inc	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

include_once('sys/itemfunctions.inc');

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("READ");
}

/**
 * appropriate page action interpretations
 */
function page_inc_execute_action($action) {
	switch($action) {
		default:
			add_alert("unknown action");
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {

	global $PAGE, $filename;
	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth']-1];

	$where = "ITEM.item_id = {$PAGE['id']} AND \n\t";
	$where .= "ITEM.item_type_id = ITEM_TYPE.item_type_id AND \n\t";
	$where .= "ITEM.author = M1.user_id AND \n\t";
	$where .= "ITEM.editor = M2.user_id AND \n\t";
	$where .= "ITEM.item_id = ITEM_IN.item_id AND \n\t";
	$where .= "ITEM_IN.sub_id = SUB_IN.sub_id AND \n\t";
	$where .= "SUB_IN.dir_id = {$PAGE['directory_tree'][$PAGE['dir_depth']-1]['dir_id']}";
	
	$sql = db_query_select(array(array("*"),array("*"),array(),array(),array("*"),"M1.nick AS nick_author, M2.nick AS nick_editor"), array("ITEM", "ITEM_TYPE", "MEMBER AS M1, MEMBER AS M2", "ITEM_IN", "SUB_IN", "DUMMY"), $where);
	$sqlresult = db_do_query($sql);
	
	if (db_num_rows($sqlresult) < 1)
		errorpage(404);
		
	$item = db_fetch_array($sqlresult);
	
	if (!has_access("MODERATE") and !has_access("ADMIN") and !($item['author'] == $_SESSION['user_id']))
		errorpage(5);
		
	switch($item['extension']) {
		case "topic":
			break;
		case "article":
			$sql = db_query_select(array(array('*')), array("ARTICLE"), "item_id = {$PAGE['id']}");
			$sqlresult = db_do_query($sql);
			$article = db_fetch_array($sqlresult);
			$item['body'] = $article['body'];
			break;
		case "link":
			$sql = db_query_select(array(array('*')), array("LINK"), "item_id = {$PAGE['id']}");
			$sqlresult = db_do_query($sql);
			$link = db_fetch_array($sqlresult);
			$item['body'] = $link['body'];
			$item['link'] = $link['link'];
			break;
		case "download":
			$sql = db_query_select(array(array('*')), array("DOWNLOAD"), "item_id = {$PAGE['id']}");
			$sqlresult = db_do_query($sql);
			$download = db_fetch_array($sqlresult);
			$item['body'] = $download['body'];
			break;
		case "poll":
			$sql = db_query_select(array(array('*')), array("POLL"), "item_id = {$PAGE['id']}");
			$sqlresult = db_do_query($sql);
			$poll = db_fetch_array($sqlresult);
			$item['body'] = $poll['body'];
			break;
		case "news":
			$sql = db_query_select(array(array('*')), array("NEWS"), "item_id = {$PAGE['id']}");
			$sqlresult = db_do_query($sql);
			$news = db_fetch_array($sqlresult);
			$item['body'] = $news['body'];
			break;
		case "gallery":
			$sql = db_query_select(array(array('*')), array("GALLERY"), "item_id = {$PAGE['id']}");
			$sqlresult = db_do_query($sql);
			$gallery = db_fetch_array($sqlresult);
			$item['body'] = $gallery['body'];
			$item['open_for_all'] = $gallery['open_for_all'];
			break;
		case "ad":
			$sql = db_query_select(array(array('*')), array("AD"), "item_id = {$PAGE['id']}");
			$sqlresult = db_do_query($sql);
			$ad = db_fetch_array($sqlresult);
			$item = array_merge($item, $ad);
			break;
		case "event":
			$sql = db_query_select(array(array('*')), array("EVENT"), "item_id = {$PAGE['id']}");
			$sqlresult = db_do_query($sql);
			$event = db_fetch_array($sqlresult);
			$item = array_merge($item, $event);
			$item['date_start'] = ereg_replace("[^0-9]", "", $item['date_start']);
			$item['date_start_year'] = substr($event['date_start'], 0, 4);
			$item['date_start_month'] = substr($event['date_start'], 4, 2);
			$item['date_start_day'] = substr($event['date_start'], 6, 2);
			$item['date_end_year'] = substr($event['date_end'], 0, 4);
			$item['date_end_month'] = substr($event['date_end'], 4, 2);
			$item['date_end_day'] = substr($event['date_end'], 6, 2);
			break;
		case "fiche":
			$sql = db_query_select(array(array('*')), array("GAME_FICHE"), "item_id = {$PAGE['id']}");
			$sqlresult = db_do_query($sql);
			$fiche = db_fetch_array($sqlresult);
			$item = array_merge($item, $fiche);
			break;
		default: 
			errorpage(8);
	}

	if ($_POST['submitted']) {
		
		// check submitted content
		if (item_page_inc_check_content($_POST, $_FILES, $item['extension'], 0)) {
		
			// if ok, edit db
			
			// derive info stuff
			switch($item['item_type_id']) {
				case 1: // plain topic
				case 8: // gallery
					$info = strip_tags($_POST['info']);
					break;
				case 2: // article
				case 4: // download
				case 5: // poll
				case 6: // news
					$info = strip_tags($_POST['body']);
					$info = substr($info, 0, 50) . ((strlen($info) > 50) ? "..." : "");
					break;
				case 9: // ad
					include_once('sys/regionfunctions.inc');
					$info = region_format(region_get_region_tree($_POST['region']));
					if (ereg("^[0-9]+$", $_POST['game_id'])) {
						$sqlresult = db_do_query(db_query_select(array(array('name')), array('GAME'), "game_id = {$_POST['game_id']}"));
						if (db_num_rows($sqlresult) == 1) {
							$g = db_fetch_array($sqlresult);
							$info = "{$g['name']} - $info";
						}
					} else if (strlen($_POST['game_name']) > 0) 
						$info = "{$_POST['game_name']} - $info";
					break;
				case 10: // event
					include_once('sys/regionfunctions.inc');
					$date = $_POST['date_start_year'] . (($_POST['date_start_month'] > 9) ? "{$_POST['date_start_month']}" : "0{$_POST['date_start_month']}") . (($_POST['date_start_day'] > 9) ? "{$_POST['date_start_day']}" : "0{$_POST['date_start_day']}") . "000000";
					$info = date_format($date, 0, -1, 1) . " - ";
					$info .= region_format(region_get_region_tree($_POST['region'])); 
					break;
				case 3: // link
					$info = substr($_POST['link'], 0, 50) . ((strlen($_POST['link']) > 50) ? "..." : "");
					break;
				case 11: // spellenfiche
					if ($_POST['game_editor_id'] == "NEW")
						$info = $_POST['editor_name'];
					else {
						$kv = get_keyed_values('game_editor_id', 'editor_name', 'GAME_EDITOR');
						$info = $kv[$_POST['game_editor_id']];
					}
					if ($_POST['game_id'] == "NEW")
						$title = $_POST['game_name'];
					else {
						$kv = get_keyed_values('game_id', 'name', 'GAME');
						$title = $kv[$_POST['game_id']];
					}
					break;
				default:
					$info = "";
					break;
			}
			$info = db_sqlize($info);
			
			$title = $title ? $title : $_POST['title'];
			
			// item update
			$sql = db_query_update(array("editor", "title", "info", "creation", "edit"), array($_SESSION['user_id'], db_sqlize($title), $info, "creation", $_POST['mark_new'] ? "NOW(14)" : "edit"), "ITEM", "item_id = {$item['item_id']}");
			db_do_query($sql);
			
			// type-specific insertion
			switch($item['item_type_id']) {
				case 1: // plain topic
					$sql = "SELECT 0";
					break;
				case 2: // article
					$sql = db_query_update(array('body'), array(db_sqlize($_POST['body'])), "ARTICLE", "item_id = {$item['item_id']}");
					break;
				case 3: // link
					$sql = db_query_update(array('link', 'body'), array(db_sqlize($_POST['link']), db_sqlize($_POST['body'])), "LINK", "item_id = {$item['item_id']}");
					break;
				case 4: // download
					if ($_POST['change_file'])
						$sql = db_query_update(array('body', 'filename'), array(db_sqlize($_POST['body']), db_sqlize($filename)), "DOWNLOAD", "item_id = {$item['item_id']}");
					else
						$sql = db_query_update(array('body'), array(db_sqlize($_POST['body'])), "DOWNLOAD", "item_id = {$item['item_id']}");
					break;
				case 5: // poll
					for ($i = 0; $i < $_POST['num_answers']; $i++) {
						$sql = db_query_update(array('answer'), array(db_sqlize($_POST["answer_{$i}"])), "POLL_ANSWER", "answer_id = {$_POST["answer_{$i}_id"]}");
						db_do_query($sql);
					}
					$sql = db_query_update(array('body'), array(db_sqlize($_POST['body'])), "POLL", "item_id = {$item['item_id']}");	
					break;
				case 6: // news
					$sql = db_query_update(array('body'), array(db_sqlize($_POST['body'])), "NEWS", "item_id = {$item['item_id']}");
					break;
				case 8: // gallery
					$sql = db_query_update(array('body', 'open_for_all'), array(db_sqlize($_POST['body']), $_POST['open_for_all'] ? 1 : 0), "GALLERY", "item_id = {$item['item_id']}");
					break;
				case 9: // ad
					if (ereg("^parent[0-9]+$", $_POST['region'])) { // new region
						db_do_query(db_query_insert(array('name'), array(db_sqlize($_POST['region_name'])), "REGION"));
						$region_id = db_insert_id();
						db_do_query(db_query_insert(array('parent_region', 'sub_region'), array(substr($_POST['region'], 6), $region_id), "REGION_TREE"));
					} else
						$region_id = $_POST['region'];
					
					if ($_POST["game_id"] == "NEW") {
						db_do_query(db_query_insert(array('name'), array(db_sqlize($_POST["game_name"])), "GAME"));
						$game_id = db_insert_id();
						db_do_query(db_query_insert(array('game_id', 'game_type_id'), array($game_id, $_POST["game_type"]), "GAME_OF_TYPE"));
					} else if ($_POST['game_id'] == "NONE")
						$game_id = "NULL";
					else
						$game_id = $_POST["game_id"];
						
					$sql = db_query_update(array('game_id', 'region_id', 'body', 'seek'), array($game_id, $region_id, db_sqlize($_POST['body']), ($_POST['seek'] == "GZ") ? 1 : 0), "AD", "item_id = {$item['item_id']}");
					break;
				case 10: // event
					if (ereg("^parent[0-9]+$", $_POST['region'])) { // new region
						db_do_query(db_query_insert(array('name'), array(db_sqlize($_POST['region_name'])), "REGION"));
						$region_id = db_insert_id();
						db_do_query(db_query_insert(array('parent_region', 'sub_region'), array(substr($_POST['region'], 6), $region_id), "REGION_TREE"));
					} else
						$region_id = $_POST['region'];
					
					$date = $_POST['date_start_year'] . (($_POST['date_start_month'] > 9) ? "{$_POST['date_start_month']}" : "0{$_POST['date_start_month']}") . (($_POST['date_start_day'] > 9) ? "{$_POST['date_start_day']}" : "0{$_POST['date_start_day']}") . "000000";	

					if ($_POST['date_end_day'])
						$enddate = $_POST['date_end_year'] . (($_POST['date_end_month'] > 9) ? "{$_POST['date_end_month']}" : "0{$_POST['date_end_month']}") . (($_POST['date_end_day'] > 9) ? "{$_POST['date_end_day']}" : "0{$_POST['date_end_day']}") . "000000";
					else
						$enddate = "NULL";

					$sql = db_query_update(array('region_id', 'description', 'link', 'date_start', 'multiple_days', 'date_end'), array($region_id, db_sqlize($_POST['description']), db_sqlize($_POST['link']), $date, $_POST['multiple_days'] ? 1 : 0, $enddate), "EVENT", "item_id = {$item['item_id']}");

					if ($_POST['archive'])
						db_do_query(db_query_insert(array('item_id', 'date_start', 'multiple_days', 'date_end', 'title'), array($item['item_id'], $item['date_start'], $item['multiple_days'], $item['date_end'], db_sqlize(db_desqlize($item['title']))), "EVENT_EDITION"));
					break;
				case 11: // spellenfiche
					
					if ($_POST["game_id"] == "NEW") {
						db_do_query(db_query_insert(array('name'), array(db_sqlize($_POST["game_name"])), "GAME"));
						$game_id = db_insert_id();
						db_do_query(db_query_insert(array('game_id', 'game_type_id'), array($game_id, $_POST["game_type"]), "GAME_OF_TYPE"));
					} else
						$game_id = $_POST["game_id"];
					
					if ($_POST["game_editor_id"] == "NEW") {
						db_do_query(db_query_insert(array('editor_name', 'editor_link'), array(db_sqlize($_POST["editor_name"]), db_sqlize($_POST["editor_link"])), "GAME_EDITOR"));
						$editor_id = db_insert_id();
					} else
						$editor_id = $_POST["game_editor_id"];

					$sql = db_query_update(array('game_id', 'game_editor_id', 'image_link', 'url', 'description', 'genre', 'rules', 'players', 'expansions', 'requirements', 'price', 'languages', 'did_you_know', 'forum_link'), array($game_id, $editor_id, $filename ?  db_sqlize($filename) : 'image_link', db_sqlize($_POST['url']), db_sqlize($_POST['description']), db_sqlize($_POST['genre']), db_sqlize($_POST['rules']), db_sqlize($_POST['players']), db_sqlize($_POST['expansions']), db_sqlize($_POST['requirements']), db_sqlize($_POST['price']), db_sqlize($_POST['languages']), db_sqlize($_POST['did_you_know']), $_POST['forum_link']), "GAME_FICHE", "item_id = {$item['item_id']}");
					
					db_do_query(db_query_update(array('link'), array(db_sqlize("{$PAGE['dirpath']}/{$item['item_id']}.fiche")), "GAME", "game_id = $game_id"));

					break;
				default:
					errorpage(8);
					break;
			}
			
			db_do_query($sql);
			
			// subset insertion
			if ($_POST['sub_id'] == "NEW") {
				$sql = db_query_insert(array('sub_title'), array(db_sqlize($_POST['new_subset'])), "SUBSET");
				db_do_query($sql);
				$sub_id = db_insert_id();
				$sql = db_query_insert(array('dir_id', 'sub_id', 'sub_title'), array($THISDIR['dir_id'], $sub_id, db_sqlize($_POST['new_subset'])), "SUB_IN");
				db_do_query($sql);
			} else {
				$sub_id = $_POST['sub_id'];
			}

			// in_sub update
			$sql = db_query_update(array('sub_id'), array($sub_id), "ITEM_IN", "item_id = {$item['item_id']} AND sub_id = {$item['sub_id']}");
			db_do_query($sql);
			
			// readings reset
			if ($_POST['mark_new']) {
				$sql = db_query_delete("HAS_READ", "item_id = {$PAGE['id']}");
				db_do_query($sql);
			}
			
			redirect($PAGE['dirpath'] . "/{$item['item_id']}." . item_page_inc_type_string($item['item_type_id']));
			
		}
		
	}
	
	$PAGE['content']['title'] = ucfirst($item['name']) ." #{$item['item_id']} aanpassen";
	
	item_form($item);
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	print_form($PAGE['content']['form']);
	// TODO previews
}

?>